export function caculateTimeago(dateTimeStamp) {
	const minute = 1000 * 60 // 把分，时，天，周，半个月，一个月用毫秒表示

	const hour = minute * 60
	const day = hour * 24
	const week = day * 7
	const now = new Date().getTime() // 获取当前时间毫秒

	const diffValue = now - dateTimeStamp // 时间差

	let result = ''

	if (diffValue < 0) {
		return
	}

	const minC = diffValue / minute // 计算时间差的分，时，天，周，月

	const hourC = diffValue / hour
	const dayC = diffValue / day
	const weekC = diffValue / week

	if (weekC >= 1 && weekC <= 4) {
		result = ` ${parseInt(weekC, 10)}周前`
	} else if (dayC >= 1 && dayC <= 6) {
		result = ` ${parseInt(dayC, 10)}天前`
	} else if (hourC >= 1 && hourC <= 23) {
		result = ` ${parseInt(hourC, 10)}小时前`
	} else if (minC >= 1 && minC <= 59) {
		result = ` ${parseInt(minC, 10)}分钟前`
	} else if (diffValue >= 0 && diffValue <= minute) {
		result = '刚刚'
	} else {
		const datetime = new Date()
		datetime.setTime(dateTimeStamp)
		const Nyear = datetime.getFullYear()
		const Nmonth =
			datetime.getMonth() + 1 < 10 ?
			`0${datetime.getMonth() + 1}` :
			datetime.getMonth() + 1
		const Ndate =
			datetime.getDate() < 10 ? `0${datetime.getDate()}` : datetime.getDate()
		result = `${Nyear}-${Nmonth}-${Ndate}`
	}

	return result
}
  
export function handleTime(data, type) {
	let result = ''
	let time = new Date(data);
	let Y = time.getFullYear();
	let Mon = time.getMonth() + 1;
	let Day = time.getDate();
	let H = time.getHours();
	let Min = time.getMinutes();
	let S = time.getSeconds();
	// const now = new Date().getTime() // 获取当前时间毫秒
	const now = new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1 // 今天的23:59:59

	console.log(data)
	console.log(time)
	const diffValue = now - data // 时间差
	console.log(diffValue)
	if (Number(H) <= 9) {
		H = '0' + H
	}
	if (Number(Min) <= 9) {
		Min = '0' + Min
	}
	if (Number(S) <= 9) {
		S = '0' + S
	}

	var tYear = new Date().getFullYear()
	var startTime = new Date(tYear, 0, 1).getTime() //当前年第一天
	var endTime = new Date(tYear, 11, 31, 23, 59, 59).getTime() //当前年最后一天

	//今天
	if (0 <= diffValue &&  diffValue <= 24 * 60 * 60 * 1000) {
		result = `${H}:${Min}`
	}
	// 昨天
	if (24 * 60 * 60 * 1000 <= diffValue && diffValue <= 2 * 24 * 60 * 60 * 1000) {
		result = `昨天 ${H}:${Min}`
	}
	//今年
	if (2 * 24 * 60 * 60 * 1000 <= diffValue && (startTime <= data && data <= endTime)) {
		result = `${Mon}月${Day}日`
	}
	//今年往前：年月日
	if (2 * 24 * 60 * 60 * 1000 <= diffValue && data < startTime) {
		result = `${Mon}月${Day}日`
	}
	return result
	//自定义选择想要返回的类型
	// if (type === "Y") { //返回年月日2020-10-10
	// 	return `${Y}-${Mon}-${Day}`
	// } else if (type === "H") { //返回时分秒20：10：10
	// 	return `${H}:${Min}:${S}`
	// } else { //返回年月日时分秒2020-10-10 10:26:38
	// 	return `${Y}-${Mon}-${Day} ${H}:${Min}:${S}`
	// }

}


export function test(date) {
	if (!date) {
		return;
	}
	// date = date * 1000
	const IN_DAY = 1000 * 60 * 60 * 24 * 1; //1天
	const Yesterday = 1000 * 60 * 60 * 24 * 2; //昨天

	let Y = dayjs(date).year(),
		M = dayjs(date).month(),
		D = dayjs(date).date(),
		// W = dayjs(date).day(),
		W = dayjs(date).format('ddd'),
		H = dayjs(date).hour(),
		Min = dayjs(date).format('mm'),
		nowY = dayjs().year(),
		nowM = dayjs().month(),
		nowD = dayjs().date(),
		timeStr = '';

	if (Y < nowY) {
		// 去年
		timeStr = `${y}/${M}/${D} `
	} else {
		if (Y === nowY && M === nowM) {
			// 昨天
			if ((nowD - D) === 1) {
				timeStr = '昨天 '
			} else if (nowD === D) {
				// 当天
				timeStr = ''
			} else {
				// 一周内
				timeStr = W + ' '
			}
		} else {
			timeStr = `${M}/${D} `
		}
	}
	timeStr += `${H}:${Min}`
	return timeStr
}

export function formateTime(secondTime) {
	const time = secondTime
	let newTime
	let hour
	let minite
	let seconds
	if (time >= 3600) {
		hour =
			parseInt(time / 3600) < 10 ?
			`0${parseInt(time / 3600)}` :
			parseInt(time / 3600)
		minite =
			parseInt((time % 60) / 60) < 10 ?
			`0${parseInt((time % 60) / 60)}` :
			parseInt((time % 60) / 60)
		seconds = time % 3600 < 10 ? `0${time % 3600}` : time % 3600
		if (seconds > 60) {
			minite =
				parseInt(seconds / 60) < 10 ?
				`0${parseInt(seconds / 60)}` :
				parseInt(seconds / 60)
			seconds = seconds % 60 < 10 ? `0${seconds % 60}` : seconds % 60
		}
		newTime = `${hour}:${minite}:${seconds}`
	} else if (time >= 60 && time < 3600) {
		minite =
			parseInt(time / 60) < 10 ? `0${parseInt(time / 60)}` : parseInt(time / 60)
		seconds = time % 60 < 10 ? `0${time % 60}` : time % 60
		newTime = `00:${minite}:${seconds}`
	} else if (time < 60) {
		seconds = time < 10 ? `0${time}` : time
		newTime = `00:00:${seconds}`
	}
	return newTime
}

// 仿微信时间显示格式转换 @time 时间戳毫秒
export function weChatTimeFormat(time) {
	const today = new Date().getTime()
	// 当前时间减去获取到的时间
	const sub = today - time
	const day = 1000 * 60 * 60 * 24
	const timeDate = new Date(time)
	const currentYear = new Date().getFullYear()
	const getYear = new Date(time).getFullYear()
	const HHmm = `${formatTime2('hh', timeDate)}:${formatTime2('mm', timeDate)}`
	const showWeekDay = isShowWeekDay(sub, timeDate.getDay())
	if (currentYear > getYear) {
		return `${formatTime2('yyyy年MM月dd日', timeDate)} ${todayTimeFormat(
      timeDate.getHours()
    )} ${formatTime2('hh:mm', timeDate)}`
	} else if (showWeekDay) {
		return `${weekFormat(timeDate.getDay())} ${HHmm}`
	} else if (sub > day && sub < day * 2) {
		return `昨天 ${HHmm}`
	} else if (sub <= day) {
		return HHmm
	} else {
		return `${formatTime2('MM月dd日', timeDate)} ${todayTimeFormat(
      timeDate.getHours()
    )} ${formatTime2('hh:mm', timeDate)}`
	}
}
